home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
webx
/
webxp040.lzh
/
Source
/
Image.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-09-27
|
11KB
|
397 lines
/* Image.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <alloca.h>
#include <sys/dos.h>
#include <sys/iocs.h>
#include <sys/xglob.h>
#include "WebXpression.h"
#include "WebCache/WebCache.h"
#include "gifl.h"
extern int GetFile (HTTPFILE *);
extern GIFLOAD *gifdecodemain (GIFLOAD * g, char *p);
extern void McPrint (char *);
extern void McDbPrint (char *);
extern void CompressImageHS (void *, void *, unsigned short, unsigned short);
extern void CompressImageHQ (void *, void *, unsigned short, unsigned short);
extern void CompressImage256HS (unsigned short *, unsigned char *,
unsigned short *, unsigned short, unsigned short);
extern void CompressImage256HQ (unsigned short *, unsigned char *,
unsigned short *, unsigned short, unsigned short);
extern void NonCompressImage256 (unsigned short *, unsigned char *,
unsigned short *, unsigned short, unsigned short);
extern void new_B_KEYINP (void), new_B_KEYSNS (void), new_BITSNS (void);
extern void new_CONCTRL (void), new_MS_INIT (void), new_MS_CUROF (void), new_SKEY_MOD (void);
extern IMAGE_LIST *image_list_top, *image_list_end, *image_list_ptr;
extern volatile signed char quit_flag;
extern XPTEXT *disp_xptext;
extern unsigned short cache_image; /* âCâüü[âWâLâââbâVâàÉö */
extern unsigned char image_quality; /* âCâüü[âWê│ÅkÄ₧é╠ëµÄ┐ */
extern unsigned char image_compress; /* âCâüü[âWé≡ê│Åké╖éΘé⌐ */
IMAGE_LIST *image_list_top = NULL, *image_list_end = NULL;
IMAGE_LIST *image_list_ptr = NULL; /* é▒éΩé⌐éτô╟é▌ì₧é▐â|âCâôâ^ */
static short node = 0; /* âmü[âhÉö */
/* ÄwÆΦé│éΩé╜âmü[âhé≡ìφÅ£é╖éΘ */
void DeleteImageNode (IMAGE_LIST * t_ptr)
{
McDbPrint ("DeleteImageNode() : ");
McDbPrint (t_ptr->url);
McDbPrint ("\n");
if (image_list_top != NULL) {
if ((t_ptr->data != NULL) && (t_ptr->data != (void *) !NULL))
_dos_mfree (t_ptr->data);
if (t_ptr->before_ptr == NULL) {
if (t_ptr->next_ptr == NULL) {
/* éPé┬é╡é⌐é╚éóâmü[âhé≡ìφÅ£ */
image_list_top = NULL;
image_list_end = NULL;
image_list_ptr = NULL;
free (t_ptr);
} else {
/* ɵô¬é╠âmü[âhé≡ìφÅ£ */
image_list_top = t_ptr->next_ptr;
(t_ptr->next_ptr)->before_ptr = NULL;
free (t_ptr);
}
} else {
if (t_ptr->next_ptr == NULL) {
/* ûûö÷é╠âmü[âhé≡ìφÅ£ */
image_list_end = t_ptr->before_ptr;
(t_ptr->before_ptr)->next_ptr = NULL;
free (t_ptr);
} else {
/* Æåè╘é╠âmü[âhé≡ìφÅ£ */
(t_ptr->before_ptr)->next_ptr = t_ptr->next_ptr;
(t_ptr->next_ptr)->before_ptr = t_ptr->before_ptr;
free (t_ptr);
}
}
}
}
/* ɵô¬é╔âmü[âhé≡éPé┬Æ╟ë┴é╖éΘ */
IMAGE_LIST *InsertImageNode (char *url)
{
IMAGE_LIST *t_ptr;
McDbPrint ("InsertImageNode() : ");
McDbPrint (url);
McDbPrint ("\n");
if ((t_ptr = malloc (sizeof (IMAGE_LIST))) == NULL) {
McDbPrint ("InsertImageNode() : ");
McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
return (NULL);
} else {
if (image_list_top == NULL) {
/* âmü[âhéOî┬é╠Åèé╔Æ╟ë┴ */
image_list_top = t_ptr;
image_list_end = t_ptr;
image_list_ptr = t_ptr;
t_ptr->before_ptr = NULL;
t_ptr->next_ptr = NULL;
} else {
/* ɵô¬é╔âmü[âhé≡Æ╟ë┴ */
image_list_top->before_ptr = t_ptr;
t_ptr->before_ptr = NULL;
t_ptr->next_ptr = image_list_top;
image_list_top = t_ptr;
}
t_ptr->x = 0;
t_ptr->y = 0;
t_ptr->data = NULL;
t_ptr->count = 1;
strcpy (t_ptr->url, url);
}
node++;
/* âLâââbâVâàé╡é─éóéΘâmü[âhé¬ cache_image é≡ëzéªé╜éτûóÄQÅ╞é╠âmü[âhé≡ìφÅ£ */
if (node > cache_image) {
IMAGE_LIST *t2_ptr;
t2_ptr = image_list_end;
while (t2_ptr != NULL) {
if ((!t2_ptr->count) && (t2_ptr != image_list_ptr) && (t2_ptr != t_ptr)) {
short i;
/* û{ôûé╔ÄQÅ╞é│éΩé─éóé╚éóé⌐â`âFâbâNüiòsùvé╚é═é╕é╛é¬êΩë₧üj */
for (i = 0; i < disp_xptext->image_table_max; i++) {
if (t2_ptr == (disp_xptext->image_table)[i].image_list)
continue;
}
DeleteImageNode (t2_ptr);
node--;
break;
}
t2_ptr = t2_ptr->before_ptr;
}
}
return (t_ptr);
}
/* url é┼ÄwÆΦé╡é╜âmü[âhé≡îƒì⌡é╖éΘ */
IMAGE_LIST *SearchImageNode (char *url)
{
IMAGE_LIST *t_ptr = image_list_top;
while (t_ptr != NULL) {
if (!strcmp (t_ptr->url, url)) {
t_ptr->count++;
return (t_ptr);
}
t_ptr = t_ptr->next_ptr;
}
return (NULL);
}
static int LoadGif (HTTPFILE * httpfile)
{
unsigned char *h = httpfile->content;
unsigned short xs, ys; /* î│ëµæ£é╠æσé½é│(Source) */
unsigned short xd, yd; /* ôWèJîπëµæ£é╠æσé½é│(Dest) */
GIFLOAD *g;
char temp_fname[256];
xs = ((unsigned short) (*(h + 7))) * 256 + (unsigned short) (*(h + 6));
ys = ((unsigned short) (*(h + 9))) * 256 + (unsigned short) (*(h + 8));
if (image_compress) {
if (!(xd = xs / 2)) /* ò¥é¬éPâhâbâgé╛é┴é╜éτ xd = 1 é╔ */
xd = 1;
if (!(yd = ys / 2)) /* ìéé│é¬éPâhâbâgé╛é┴é╜éτ yd = 1 é╔ */
yd = 1;
} else {
xd = xs;
yd = ys;
}
/* temp_fname é≡ô╛éΘé╛é» */
WCExist (httpfile, temp_fname);
if ((int) (image_list_ptr->data = _dos_malloc (xd * yd * 2)) < 0) {
McDbPrint ("LoadGif() : ");
McPrint ("üª .GIF data ùpâüâéâèé¬æ½éΦé▄é╣é±\n");
image_list_ptr->data = (void *) !NULL;
return (-1);
}
if ((int) (g = _dos_malloc (sizeof (GIFLOAD))) < 0) {
McDbPrint ("LoadGif() : ");
McPrint ("üª .GIF âÅü[âNùpâüâéâèé¬æ½éΦé▄é╣é±\n");
_dos_mfree (image_list_ptr->data);
image_list_ptr->data = (void *) !NULL;
return (-1);
} { /* âÅü[âNé≡âNâèâAüié▒é±é╚Æxéóò√û@é┼üEüEüEüj */
char *p = (char *) g;
int i;
for (i = 0; i < sizeof (GIFLOAD); i++)
*p++ = '\0';
}
g->tpcolor = -1;
g->addr = (void *) -1;
McPrint (".GIF é≡ôWèJé╡é─éóé▄é╖...\n");
if ((int) (gifdecodemain (g, temp_fname)) < 0) {
McDbPrint ("LoadGif() : ");
McPrint ("üª .GIF é╠âfâRü[âhé¬é┼é½é▄é╣é±é┼é╡é╜\n");
if ((int) g->addr > 0)
_dos_mfree (g->addr);
_dos_mfree (g);
_dos_mfree (image_list_ptr->data);
image_list_ptr->data = (void *) !NULL;
return (-1);
}
if ((int) g->addr < 0) {
McDbPrint ("LoadGif() : ");
McPrint ("üª .GIF ôWèJùpâüâéâèé¬æ½éΦé▄é╣é±\n");
_dos_mfree (g);
_dos_mfree (image_list_ptr->data);
image_list_ptr->data = (void *) !NULL;
return (-1);
}
if (image_compress) {
if (!image_quality)
CompressImage256HS (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
else
CompressImage256HQ (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
_dos_mfree (g->addr);
_dos_mfree (g);
} else {
NonCompressImage256 (image_list_ptr->data, g->addr, &g->pal_buf[0], xs, ys);
_dos_mfree (g->addr);
_dos_mfree (g);
}
image_list_ptr->x = xd;
image_list_ptr->y = yd;
return (0);
}
static int LoadJpeg (HTTPFILE * httpfile)
{
char cmdline[256];
FILE *fp;
unsigned short xs = 0, ys = 0; /* î│ëµæ£é╠æσé½é│(Source) */
unsigned short xd, yd; /* ôWèJîπëµæ£é╠æσé½é│(Dest) */
char temp_fname[256];
unsigned short *temp_image;
void *old_CONCTRL, *old_MS_INIT, *old_MS_CUROF, *old_SKEY_MOD;
char progressive = 0;
/* temp_fname é≡ô╛éΘé╛é» */
WCExist (httpfile, temp_fname);
/* JPEG âtâ@âCâïé╠ìéé│ü^ëíò¥é≡ô╛éΘ */
/* vwx é╠â\ü[âXé╠ file.c é≡ÄQìlé╔é╡é▄é╡é╜üiæ╜Ä╙üj */
if ((fp = fopen (temp_fname, "rb")) != NULL) {
fseek (fp, 2, SEEK_SET);
while (!feof (fp)) {
unsigned char buf[8];
int c;
if ((c = fgetc (fp)) != 0xff)
continue;
c = fgetc (fp);
if ((c != 0xc0) && (c != 0xc2)) {
c = fgetc (fp) * 256 + fgetc (fp);
fseek (fp, c - 2, 1);
continue;
}
if (c == 0xc2) { /* âvâìâOâîâbâVâu JPEG üH */
progressive = !0;
}
fread (buf + 1, 7, sizeof (char), fp);
ys = *(unsigned short *) (buf + 4);
xs = *(unsigned short *) (buf + 6);
break;
}
fclose (fp);
}
if (!progressive) {
/* Baseline JPEGüiòüÆ╩é╠ JPEGüjé╠ÅΩìç */
/* JPEGED.R é¬ëµû╩éΓâ}âEâXé≡Åëè·ë╗é╡é╚éóéµéñé╔ */
old_CONCTRL = _dos_intvcs (0xff23, new_CONCTRL);
old_MS_INIT = _dos_intvcs (0x170, new_MS_INIT);
old_MS_CUROF = _dos_intvcs (0x172, new_MS_CUROF);
old_SKEY_MOD = _dos_intvcs (0x17d, new_SKEY_MOD);
if (image_compress) {
if ((xd = xs / 2) == 0) /* ò¥é¬éPâhâbâgé╛é┴é╜éτ xd = 1 é╔ */
xd = 1;
if ((yd = ys / 2) == 0) /* ìéé│é¬éPâhâbâgé╛é┴é╜éτ yd = 1 é╔ */
yd = 1;
} else {
xd = xs;
yd = ys;
}
if ((int) (temp_image = _dos_malloc (xs * ys * 2)) > 0) {
sprintf (cmdline, "-VS%d,%d,$%p", xs, ys, temp_image);
McPrint (".JPG é≡ôWèJé╡é─éóé▄é╖...\n");
if (!spawnlp (P_WAIT, "JPEGED.R", "JPEGED.R", cmdline, temp_fname, NULL)) {
if (image_compress) {
if ((int) (image_list_ptr->data = _dos_malloc (xd * yd * 2)) > 0) {
if (!image_quality)
CompressImageHS (image_list_ptr->data, temp_image, xs, ys);
else
CompressImageHQ (image_list_ptr->data, temp_image, xs, ys);
image_list_ptr->x = xd;
image_list_ptr->y = yd;
} else {
McDbPrint ("LoadJpeg() : ");
McPrint ("üª .JPG data ùpâüâéâèé¬æ½éΦé▄é╣é±\n");
image_list_ptr->data = (void *) !NULL;
}
} else {
image_list_ptr->data = temp_image;
temp_image = NULL;
image_list_ptr->x = xd;
image_list_ptr->y = yd;
}
} else {
McDbPrint ("LoadJpeg() : ");
McPrint ("üª JPEGED.R é┼âGâëü[é¬ö¡É╢é╡é▄é╡é╜\n");
}
if (temp_image != NULL)
_dos_mfree (temp_image);
} else {
McDbPrint ("LoadJpeg() : ");
McPrint ("üª .JPG ôWèJùpâüâéâèé¬æ½éΦé▄é╣é±\n");
_dos_mfree (image_list_ptr->data);
image_list_ptr->data = (void *) !NULL;
}
_dos_intvcs (0x170, old_MS_INIT);
_dos_intvcs (0x172, old_MS_CUROF);
_dos_intvcs (0x17d, old_SKEY_MOD);
_dos_intvcs (0xff23, old_CONCTRL);
} else {
/* Progressive JPEG é╠ÅΩìç */
McPrint ("üª âvâìâOâîâbâVâuJPEGé╔é═ûóæ╬ë₧é┼é╖\n");
}
return (0);
}
/* âCâüü[âWâèâXâgô╟é▌ì₧é▌ */
/* QUIT_NON Ä₧î─é╤Åoé│éΩéΘâïü[â`âô */
int LoadImage (void)
{
HTTPFILE *httpfile;
/* âXâ^âbâNâtâîü[âÇé⌐éτèmò█ */
if ((httpfile = alloca (sizeof (HTTPFILE))) == NULL) {
McDbPrint ("LoadImage() : ");
McPrint ("üª âüâéâèé¬æ½éΦé▄é╣é±\n");
return (-1);
}
image_list_ptr->x = 16; /* ë╝é╔èäéΦôûé─éΘâTâCâY */
image_list_ptr->y = 16;
image_list_ptr->data = (void *) !NULL; /* âfü[â^é¬û│éóÄ₧é╠âfâtâHâïâg */
strcpy (httpfile->url, image_list_ptr->url);
if (GetFile (httpfile) < 0) {
McDbPrint ("LoadImage() : ");
McPrint (image_list_ptr->url);
McPrint ("é¬ô╟é▌ì₧é▀é▄é╣é±é┼é╡é╜\n");
image_list_ptr->data = (void *) !NULL; /* âfü[â^é¬û│éóÄ₧é╠âfâtâHâïâg */
/* free (httpfile); alloca é╔é┬é½òsùv */
return (-1);
} else {
McDbPrint ("LoadImage() : ");
McDbPrint (httpfile->url);
McDbPrint (" é≡Åêù¥é╡é▄é╖\n");
if (!strcmp (httpfile->content_type, "image/gif")) {
LoadGif (httpfile);
} else {
if (!strcmp (httpfile->content_type, "image/jpeg")) {
LoadJpeg (httpfile);
}
}
_dos_mfree (httpfile->content);
/* free (httpfile); alloca é╔é┬é½òsùv */
}
return (0);
}